package com.ssyc.student.activity;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.view.NoScrollViewPager;
import com.ssyc.student.R;
import com.ssyc.student.fragment.StudentFashionableDressFragment;
import com.ssyc.student.fragment.StudentFoodFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--百宝箱
 * Created by Administrator on 2018/4/12 0012.
 */

public class StudentTreasureChestActivity extends BaseActivity implements View.OnClickListener {

    //不可滚动NoScrollViewPager控件
    private NoScrollViewPager vp;
    //适配器
    private VpCommonAdapter adapter;

    //填充的Fragment集合
    private List<Fragment> mFragments;

    //美食
    private ImageView lvFood;

    //返回
    private ImageView iv_close;

    //服装
    private ImageView lvClothing;

    //背景
    private ImageView ib_background;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.student_food_select, R.drawable.student_clothing_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.student_food_normal, R.drawable.student_clothing_normal};

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_treasure_chest;
    }

    @Override
    protected void init() {
        showContent();
        //初始化视图
        initView();
        //设置ViewPager
        initVp();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {

        iv_close = findViewById(R.id.rl_close);

        iv_close.setOnClickListener(this);

        lvFood = findViewById(R.id.lv_food);

        lvFood.setOnClickListener(this);

        lvClothing = findViewById(R.id.lv_clothingandprop);

        lvClothing.setOnClickListener(this);

        vp = findViewById(R.id.vp);

        ib_background = findViewById(R.id.ib_background);
    }

    /**
     * 设置ViewPager的方法
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(StudentFoodFragment.newInstance());
        mFragments.add(StudentFashionableDressFragment.newInstance());
        adapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setAdapter(adapter);
        vp.setPagerEnabled(false);
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_close) {
            //关闭页面
            finish();
        } else if (id == R.id.lv_food) {
            //食物
            syncVpState(0);

            ib_background.setImageDrawable(this.getResources().getDrawable(R.drawable.student_bg));
        } else if (id == R.id.lv_clothingandprop) {
            //服装
            syncVpState(1);
            ib_background.setImageDrawable(this.getResources().getDrawable(R.drawable.student_treasure_background));
        }
    }

    /**
     * 同步ViewPager页面和切换选中Tab的方法
     *
     * @param pos
     */
    private void syncVpState(int pos) {
        vp.setCurrentItem(pos);
        if (pos == 0) {
            lvFood.setImageResource(tabSelectIds[0]);
            lvClothing.setImageResource(tabUnselectIds[1]);
        } else if (pos == 1) {
            lvFood.setImageResource(tabUnselectIds[0]);
            lvClothing.setImageResource(tabSelectIds[1]);
        }
    }

    /**
     * 通过EventBus更改当前页面的余额
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);

        String url = busInfo.msg;
        int type = busInfo.pos;
        String id = busInfo.answerFlag;

//      UiUtils.Toast("url:"+url+",id:"+id,false);
        if(url!=null){
        if ("-1".equals(url)) {
            ib_background.setImageDrawable(this.getResources().getDrawable(R.drawable.student_treasure_background));
        }else{
            Glide.with(this).load(url).into(ib_background);
        }
        }
    }
}
